package com.jxtti.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jxtti.dao.SiteMenuDao;
import com.jxtti.entity.SiteMenu;

@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class SiteMenuService {
	private final static Logger log= Logger.getLogger(SiteMenuService.class);	

	@Autowired
    private SiteMenuDao<SiteMenu> SiteMenuDao;
	
	
	public List<SiteMenu>  queryAll(Map<String, Object> data)
	{
		return  SiteMenuDao.queryByList(data);
	}	
	
	
	public List<SiteMenu>  findByPid(Map<String, Object> data)
	{
		return  SiteMenuDao.findByPid(data);
	}	
	
	public List<SiteMenu>  findPidAndPid(Map<String, Object> data)
	{
		return  SiteMenuDao.findPidAndPid(data);
	}
		
	/**
	 * 返回easyui数结构的组织架构
	 * 
	 * @param data
	 * @return
	 */
	public List<Map<String, Object>> getEasyUiTree(Map<String, String> data) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<SiteMenu> list=new ArrayList<SiteMenu>(); 
		
		if (null == data.get("parentID")) {
			data.put("parentID", "0");
		}	
		
		list.addAll(SiteMenuDao.getTreeNodes(data));
		
		if (null != list && 0 != list.size()) { //判断是否有数据
			for (int i = 0; i < list.size(); i++) { //循环数据
				
				Map<String, Object> node = new HashMap<String, Object>();					
				node.put("id", list.get(i).getId()); //是 getCode	
				node.put("parentID", list.get(i).getParentID());	
				node.put("menuName", list.get(i).getMenuName());					
				node.put("url", list.get(i).getUrl());
				node.put("sortID", list.get(i).getSortID());
				node.put("icon", list.get(i).getIcon());				
				node.put("issue", list.get(i).getIssue());		
				node.put("typeID", list.get(i).getTypeID());
				node.put("categoryID", list.get(i).getCategoryID());
				node.put("target", list.get(i).getTarget());
				
//				if ("true".equals(list.get(i).getIsParent())) { //是父级节点
//					node.put("state","closed");	 //异步加载	 		
//				}else{
//					node.put("state","open");		
//				}				
				
				/** 自定义属性
				    Map<String, Object> attrNode = new HashMap<String, Object>();							
					attrNode.put("code", list.get(i).getCode());				
					node.put("attributes",attrNode);
				 */				
				if ("true".equals(list.get(i).getIsParent())) { //是父级节点
					// 区域节点，需要展开下级
					List<Map<String, Object>> childResult = new ArrayList<Map<String, Object>>();
					
					Map<String, String> childData=data; //传递 type 和status
					System.out.println(list.get(i).getId());
					childData.put("parentID", list.get(i).getId().toString());//参数 父类 改变了	
					
					
					childResult=getEasyUiTree(childData);//---------------递归 			
		
					node.put("children", childResult);
				}				
				result.add(node);
			}
		}
		return result;
	}
	
	
	public int add(SiteMenu tCityAQIWrite)
	{
		int insertCount = SiteMenuDao.add(tCityAQIWrite);
		return insertCount;
	}	

	
	public int update(SiteMenu tCityAQIWrite)
	{
		int updateCount = SiteMenuDao.update(tCityAQIWrite);
		return updateCount;
	}	

	
	public int delete(SiteMenu entity)
	{
		int deleteCount = SiteMenuDao.delete(entity);		
		return deleteCount;
	}
	
	
	public SiteMenu queryById(SiteMenu entity)
	{
		entity = SiteMenuDao.queryById(entity);
		return entity;
	}

}
